from django.contrib.gis.db import models
from django.contrib.auth.models import User
from templatetags import image_resize



class MapKey(models.Model):
    """A MapKey is a customized graphic/text on a map."""
    unique_name = models.CharField(max_length=30, unique=True)
    title = models.CharField(max_length=50)
    description = models.TextField(null=True)
    graphic = models.ImageField(upload_to='map_keys')
    
    def __unicode__ (self):
        return self.unique_name


class MapLayer(models.Model):
    """
    A MapLayer is a dynamic layer, comprised of numerous MapObjects, that
    can be retrieved as KML data, and overlayed on a map.
    """
    unique_name = models.CharField(max_length=30, unique=True)
    title = models.CharField(max_length=40)
    created_by = models.ForeignKey(User, null=True)

    def __unicode__ (self):
        return self.unique_name


class StaticMapLayer(models.Model):
    """StaticMapLayer is a layer that is represented by an uploaded KML file"""
    unique_name = models.CharField(max_length=30, unique=True)
    title = models.CharField(max_length=40)
    created_by = models.ForeignKey(User, null=True)
    kml_file = models.FileField(upload_to='static_kml')
    map_keys = models.ManyToManyField(MapKey, blank=True, null=True)

    def __unicode__ (self):
        return self.unique_name


class Map(models.Model):
    """A Map consists of a list of layers, zoom elements, etc..."""
    unique_name = models.CharField(max_length=30, unique=True)
    title = models.CharField(max_length=50)
    caption = models.TextField(null=True)
    static_layers = models.ManyToManyField(StaticMapLayer, blank=True, null=True)
    dynamic_layers = models.ManyToManyField(MapLayer, blank=True, null=True)
    created_by = models.ForeignKey(User, null=True)
    center = models.PointField(srid=4326)
    zoom = models.DecimalField(max_digits=2, decimal_places=0, default=10)
    objects = models.GeoManager()


class MapObject(models.Model):
    """
    A MapObject is a base class that contains commom data elements used by most
    other objects on the map.
    """
    title = models.CharField(max_length=50)
    caption = models.TextField(null=True)
    long_caption = models.TextField(null=True)
    created_by = models.ForeignKey(User, null=True)
    private = models.BooleanField(default=False)
    layers = models.ManyToManyField(MapLayer, null=True)
    
    def __unicode__ (self):
        return self.title
    
    class Meta:
        abstract = True
    
    def getGeometry(self):
        pass


class PointOfInterest(MapObject):
    loc = models.PointField(srid=4326)
    objects = models.GeoManager()
    
    def getGeometry(self):
        return self.loc


class Route(MapObject):
    track = models.MultiLineStringField(srid=4326, null=True) # Full detail
    track_simplified_coarse = models.MultiLineStringField(srid=4326, null=True) # Simplified for far-zoomed-out maps
    track_simplified_fine = models.MultiLineStringField(srid=4326, null=True) # Simplified for medium-zoomed-out maps
    objects = models.GeoManager()
    
    def getGeometry(self):
        return self.track


class Area(MapObject):
    mpoly = models.MultiPolygonField(srid=4326)
    objects = models.GeoManager()
    
    def getGeometry(self):
        return self.mpoly


class Photo(MapObject):
    rank = models.DecimalField(default=0, decimal_places=0, max_digits=2)
    location = models.PointField(null=True, srid=4326)
    timetaken = models.DateTimeField(null=True)
    subjectlocation = models.PointField(null=True, srid=4326)
    image = models.ImageField(upload_to='Images/Fullres')
    photographer = models.CharField(null=True, max_length=128)
    objects = models.GeoManager()
    
    def getGeometry(self):
        return self.location


class TrackPoint(models.Model):
    """
    TrackPoints are hidden elements, they make up the points in a route.  It is
    worth keeping them around for now so we can shift photos around, edit
    routes, etc...
    """
    # Lon-Lat(-Ele) Projection SRID 4326
    location = models.PointField(srid=4326)
    location_trusted = models.BooleanField(default=False)
    time = models.DateTimeField(null=True)
    time_trusted = models.BooleanField(default=False)
    time_guessed = models.BooleanField(default=False)
    image = models.ImageField(upload_to='Images/Fullres', null=True)
    image_id = models.DecimalField(max_digits=7, decimal_places=0, null=True)
    objects = models.GeoManager()
    route = models.ForeignKey(Route) # This is the route that the point belongs to.
    user = models.ForeignKey(User, null=True)

    def __unicode__ (self):
        return "(" + str(self.location.x) + ", " + str(self.location.y) + ")"
